package fr.emse.tatiana.corpus;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Wizard page shown when the user has chosen plane as means of 
 * transport
 */

public class AddFileSynchroPage extends WizardPage implements ModifyListener {

	private Text replayer;
	private Label replayerlabel;
	private Text delta;	
	private Text start;
	private Text end;

	private IStatus replayerstatus;
	private IStatus deltastatus;
	private IStatus startstatus;
	private IStatus endstatus;
	
	private CorpusFile cf;

	/**
	 * Constructor for AddFileSynchroPage.
	 */
	public AddFileSynchroPage(String s) {
		super(s);
		setTitle("Add File To Corpus");
		setDescription("Specify synchronisation information");
		replayerstatus = new Status(IStatus.ERROR, "not_used", 0, "Please enter a replayer", null);
		deltastatus = new Status(IStatus.OK, "not_used", 0, "", null);
		startstatus = new Status(IStatus.OK, "not_used", 0, "", null);
		endstatus = new Status(IStatus.OK, "not_used", 0, "", null);
	}



	/**

	 * @see IDialogPage#createControl(Composite)

	 */

	public void createControl(Composite parent) {
	    // create the composite to hold the widgets
		GridData gd;
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		int ncol = 2;
		gl.numColumns = ncol;
		composite.setLayout(gl);

		
		replayerlabel = new Label(composite, SWT.NONE);
		replayerlabel.setText("Replayer");
		replayer = new Text(composite, SWT.BORDER);
		replayer.addModifyListener(this);
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		replayer.setLayoutData(gd);
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("First Timestamp");
		start = new Text(composite, SWT.BORDER);
		start.addModifyListener(this);
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		start.setLayoutData(gd);
		
		label = new Label(composite, SWT.NONE);
		label.setText("Last Timestamp");
		end = new Text(composite, SWT.BORDER);
		end.addModifyListener(this);
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		end.setLayoutData(gd);
		
		label = new Label(composite, SWT.NONE);
		label.setText("Delta");
		delta = new Text(composite, SWT.BORDER);
		delta.addModifyListener(this);
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		delta.setLayoutData(gd);
		
	    // set the composite as the control for this page

		setControl(composite);	
	}

	@Override
	public boolean canFlipToNextPage()
	{
		// no next page for this path through the wizard
		return false;
	}
	

	/*
	 * Sets the completed field on the wizard class when all the information 
	 * is entered and the wizard can be completed
	 */	 

	@Override
	public boolean isPageComplete()
	{
		IStatus status = findMostSevere();
		return !status.matches(IStatus.ERROR);
	}

	void onEnterPage()
	{
	    // Gets the model

	    AddFileWizard wizard = (AddFileWizard)getWizard();
		cf = wizard.cf;

		replayer.setText(cf.getReplayer());
		delta.setText(""+cf.getDelta());
		start.setText(""+cf.getFirstTime());
		end.setText(""+cf.getLastTime());
		
		if (cf.getType()==CorpusFile.VIDEO) {
			replayer.setVisible(false);
			replayerlabel.setText("This is a video file");
			replayerlabel.pack(true);
			deltastatus = new Status(IStatus.WARNING, "not_used", 0, "Delta probably shouldn't be 0", null);
			endstatus = new Status(IStatus.WARNING, "not_used", 0, "End time probably shouldn't be 0",null);
		}
		
		setPageComplete(isPageComplete());
		applyToStatusLine(findMostSevere());
		getWizard().getContainer().updateButtons();
	}



	public void modifyText(ModifyEvent e) {
		if (e.widget == replayer) {
			if ("".equals(replayer.getText())) {
				replayerstatus = new Status(IStatus.ERROR, "not_used", 0, "Please name a replayer",null);
			} else {
				replayerstatus = new Status(IStatus.OK, "not_used", 0, "",null);
				cf.setReplayer(replayer.getText());
			}
		} else {
			try {
				long l = Long.parseLong(((Text)e.widget).getText());
				if (e.widget == delta) {
					deltastatus = new Status(IStatus.OK, "not_used", 0, "",null);
					cf.setDelta(l);
				} else if (e.widget == start) {
					startstatus = new Status(IStatus.OK, "not_used", 0, "",null);
					cf.setFirstTime(l);
				} else {
					endstatus = new Status(IStatus.OK, "not_used", 0, "",null);
					cf.setLastTime(l);
				}
			} catch (NumberFormatException nfe) {
				if (e.widget == delta) {
					deltastatus  = new Status(IStatus.ERROR, "not_used", 0, "Incorrect delta",null);
				} else if (e.widget == start) {
					startstatus  = new Status(IStatus.ERROR, "not_used", 0, "Incorrect start time",null);
				} else {
					endstatus = new Status(IStatus.ERROR, "not_used", 0, "Incorrect end time",null);
				}
			}
		}

		setPageComplete(isPageComplete());
		applyToStatusLine(findMostSevere());
		getWizard().getContainer().updateButtons();
	}
	
	/**

	 * Applies the status to the status line of a dialog page.

	 */

	private void applyToStatusLine(IStatus status) {
		String message= status.getMessage();
		if (message.length() == 0) message= null;
		switch (status.getSeverity()) {
			case IStatus.OK:
				setErrorMessage(null);
				setMessage(message);
				break;
			case IStatus.WARNING:
				setErrorMessage(null);
				setMessage(message, IMessageProvider.WARNING);
				break;				
			case IStatus.INFO:
				setErrorMessage(null);
				setMessage(message, IMessageProvider.INFORMATION);
				break;			
			default:
				setErrorMessage(message);
				setMessage(null);
				break;		
		}

	}	

	

	private IStatus findMostSevere()
	{
		if (replayerstatus.matches(IStatus.ERROR))
			return replayerstatus;
		if (deltastatus.matches(IStatus.ERROR))
			return deltastatus;
		if (startstatus.matches(IStatus.ERROR))
			return startstatus;
		else return endstatus;	
	}

}